package com.sdry.project.controller;
import java.time.LocalDateTime;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.project.entity.ProjectEntity;
import com.sdry.project.entity.ProjectExpert2023;
import com.sdry.project.entity.ProjectExpertEntity;
import com.sdry.project.service.IProjectExpert2023Service;
import com.sdry.project.service.ProjectExpertService;
import com.sdry.project.service.ProjectService;
import com.sdry.project.service.impl.ProjectExpert2023ServiceImpl;
import com.sdry.system.entity.RoleUser;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.UserService;
import com.sdry.untils.StringUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  项目专家分配
 * </p>
 *
 * @author chenBin
 * @since 2020-08-10
 */
@Controller
@RequestMapping("projectExpert")
public class ProjectExpertController extends BaseController {

    @Autowired
    private ProjectExpertService projectExpertService;

    @Autowired
    private IProjectExpert2023Service projectExpert2023Service;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private UserService userService;

    /**
     * 项目分配
     * @param map 参数
     * @return getDataTable
     */
    @RequestMapping(value = "queryPage")
    @ResponseBody
    public TableDataInfo queryPage(@RequestParam HashMap<String,Object> map) {
        //PageHelper.startPage(Integer.parseInt(map.get("page").toString()), Integer.parseInt(map.get("limit").toString()));
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ProjectEntity::getPname,map.get("projectName"));
                //.orderByAsc(ProjectEntity::getAwardsresult);
        //List<ProjectEntity> list = projectService.list(queryWrapper);
        List<ProjectEntity> list = projectService.listByExpoer2021(map.get("projectName"),null);
        //PageInfo<ProjectEntity> pageInfo = new PageInfo<>(list);
        for (ProjectEntity projectEntity : list) {
            String realNames = projectExpertService.listUserRelName(projectEntity.getId());
            projectEntity.setExpert(realNames);
        }

        /*list.sort(new Comparator<ProjectEntity>() {
            @Override
            public int compare(ProjectEntity projectEntity, ProjectEntity t1) {
                String awardsResult2 = projectEntity.getAwardsresult();
                String awardsResult1 = t1.getAwardsresult();
                if (StringUtil.isEmpty(awardsResult2)) {
                    awardsResult2 = "9";
                }
                if (StringUtil.isEmpty(awardsResult1)) {
                    awardsResult1 = "9";
                }
                int i2 = 0;
                int i1 = 0;
                try {
                    i2 = Integer.parseInt(awardsResult2);
                    i1 = Integer.parseInt(awardsResult1);
                }catch (Exception e) {
                    i1 = 0;
                    i2 = 0;
                }
                return i2 - i1;
            }
        });*/

        return getDataTable(list,100000L);
    }

    /**
     * 所有的专家
     * @return
     */
    @RequestMapping(value = "queryList")
    @ResponseBody
    public List<SysUser> queryList() {
        QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(SysUser::getDelFlag,0).eq(SysUser::getLogOn,0);
        List<SysUser> list = userService.list(queryWrapper1);
        for (SysUser user : list) {
            QueryWrapper<ProjectExpertEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ProjectExpertEntity::getUserId,user.getUserId())
                    .eq(ProjectExpertEntity::getDelFlag,0);
            int count = projectExpertService.count(queryWrapper);
            user.setRemarkTwo(count+"");
        }

        list.sort(new Comparator<SysUser>() {
            @Override
            public int compare(SysUser user, SysUser t1) {
                if (StringUtil.isEmpty(user.getRemarkTwo())) {
                    user.setRemarkTwo("0");
                }
                if (StringUtil.isEmpty(t1.getRemarkTwo())) {
                    t1.setRemarkTwo("0");
                }

                int i1 = Integer.parseInt(user.getRemarkTwo());
                int i2 = Integer.parseInt(t1.getRemarkTwo());
                return i1 - i2;
            }
        });

        return list;
    }

    /**
     * 根据项目ID查询所有的专家
     * @return
     */
    @RequestMapping(value = "queryListByProjectId")
    @ResponseBody
    public List<SysUser> queryListByProjectId(Long projectId) {
        QueryWrapper<ProjectExpertEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProjectExpertEntity::getProjectId,projectId)
                .eq(ProjectExpertEntity::getDelFlag,0);
        List<ProjectExpertEntity> list = projectExpertService.list(queryWrapper);
        List<Long> longList = new ArrayList<>();
        for (ProjectExpertEntity projectExpertEntity : list) {
            longList.add(projectExpertEntity.getUserId());
        }

        //比较是否被选择
        QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(SysUser::getDelFlag,0).eq(SysUser::getLogOn,0);
        List<SysUser> list1 = userService.list(queryWrapper1);

        for (SysUser sysUser : list1) {
            if (longList.contains(sysUser.getUserId())) {
                sysUser.setRemarkOne("0");
            }
            QueryWrapper<ProjectExpertEntity> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.lambda().eq(ProjectExpertEntity::getUserId,sysUser.getUserId())
                    .eq(ProjectExpertEntity::getDelFlag,0);
            int count = projectExpertService.count(queryWrapper4);
            sysUser.setRemarkTwo(count+"");
        }

        list1.sort((user, t1) -> {
            if (StringUtil.isEmpty(user.getRemarkTwo())) {
                user.setRemarkTwo("0");
            }
            if (StringUtil.isEmpty(t1.getRemarkTwo())) {
                t1.setRemarkTwo("0");
            }

            int i1 = Integer.parseInt(user.getRemarkTwo());
            int i2 = Integer.parseInt(t1.getRemarkTwo());
            return i1 - i2;
        });

        return list1;
    }

    /**
     * 查询项目下所有的专家
     * @return
     */
    @RequestMapping(value = "queryExpertList")
    @ResponseBody
    public TableDataInfo queryList(Long projectId) {
        QueryWrapper<ProjectExpertEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProjectExpertEntity::getProjectId,projectId)
                .eq(ProjectExpertEntity::getDelFlag,0);
        List<ProjectExpertEntity> list = projectExpertService.list(queryWrapper);
        for (ProjectExpertEntity projectExpertEntity : list) {
            SysUser user = userService.getById(projectExpertEntity.getUserId());
            if (user != null) {
                projectExpertEntity.setRealName(user.getRealName());
            }
        }
        return getDataTable(list,0L);
    }

    /**
     * 添加
     * @param projectId 项目ID
     * @param userId 专家ID
     * @return boolean
     */
    @RequestMapping(value = "add")
    public boolean add(Long projectId,String userId) {
        try {
            return projectExpertService.add(projectId,userId);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 添加
     * @param projectIds 项目ID
     * @param userIds 专家ID
     * @return boolean
     */
    @RequestMapping(value = "add2023")
    public boolean add2023(String projectIds,String userIds) {
        try {
            return projectExpertService.add2023(projectIds,userIds);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改
     * @param projectId 项目ID
     * @param userId 专家ID
     * @return boolean
     */
    @RequestMapping(value = "update")
    public boolean update(Long projectId,String userId) {
        try {
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 管理员撤回
     * @param id id
     * @return boolean
     */
    @RequestMapping(value = "updateExpertRecall")
    public boolean updateExpertRecall(Long id) {
        try {
            return projectExpertService.updateExpertRecall(id);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 管理员撤回
     * @param id id
     * @return boolean
     */
    @RequestMapping(value = "updateExpertRecall2021")
    public boolean updateExpertRecall2021(Long id) {
        try {
            return projectExpertService.updateExpertRecall2021(id);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 发起下一次评审
     * @return
     */
    @RequestMapping(value = "approvalNext")
    public boolean approvalNext(String projectIds) {
        try {
            return projectExpertService.approvalNext(projectIds);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
